extends CharacterBody3D


# Emitted when the player jumped on the mob.
signal squashed

# Minimum speed of the mob in meters per second.
@export var min_speed = 10
# Maximum speed of the mob in meters per second.
@export var max_speed = 18


func _physics_process(_delta):
	move_and_slide()


# We will call this function from the Main scene.
func initialize(start_position, player_position):
	#position = start_position
	# We turn the mob so it looks at the player.
	#look_at(player_position, Vector3.UP)
	look_at_from_position(start_position, player_position)
	# And rotate it randomly so it doesn't move exactly toward the player.
	rotate_y(randf_range(-PI / 4, PI / 4))
	
	# We calculate a random speed.
	var random_speed = randf_range(min_speed, max_speed)
	# We calculate a forward velocity that represents the speed.
	motion_velocity = Vector3.FORWARD * random_speed
	# We then rotate the vector based on the mob's Y rotation to move in the direction it's looking.
	motion_velocity = motion_velocity.rotated(Vector3.UP, rotation.y)


func squash():
	emit_signal("squashed")
	queue_free()


func _on_VisibleOnScreenNotifier3D_screen_exited():
	queue_free()
